package jnpf.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.base.UserInfo;
import jnpf.config.PermissionsConfig;
import jnpf.entity.ZXGL_RenWuZhuanBanEntity;
import jnpf.entity.ZXGL_XiangMuFenJie_mingxibiaoEntity;
import jnpf.mapper.ZXGL_RenWuZhuanBanMapper;
import jnpf.model.zxgl_renwuzhuanban.ZXGL_RenWuZhuanBanInfoVO;
import jnpf.model.zxgl_renwuzhuanban.ZXGL_RenWuZhuanBanPagination;
import jnpf.permission.entity.OrganizeEntity;
import jnpf.permission.service.AuthorizeService;
import jnpf.service.ZXGL_RenWuZhuanBanService;
import jnpf.service.ZXGL_XiangMuFenJie_mingxibiaoService;
import jnpf.util.DateUtil;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ZXGL_RenWuZhuanBan
 * 版本： V2.0
 * 版权： 河北交投智能交通技术有限责任公司
 * 作者： 数字交投项目组
 * 日期： 2025-03-22
 */
@Service
public class ZXGL_RenWuZhuanBanServiceImpl extends ServiceImpl<ZXGL_RenWuZhuanBanMapper, ZXGL_RenWuZhuanBanEntity> implements ZXGL_RenWuZhuanBanService {


    @Autowired
    private UserProvider userProvider;

    @Autowired
    private AuthorizeService authorizeService;

    @Autowired
    private ZXGL_XiangMuFenJie_mingxibiaoService zxglXiangMuFenJieMingxibiaoService;
    @Resource
    PermissionsConfig permissionsConfig;

    @Override
    public List<ZXGL_RenWuZhuanBanEntity> getList(ZXGL_RenWuZhuanBanPagination zXGL_RenWuZhuanBanPagination) {

        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_RenWuZhuanBanEntity> zXGL_RenWuZhuanBanQueryWrapper = new QueryWrapper<>();
        //TODO 增加权限逻辑
        UserInfo userInfo = userProvider.get();
        if (!userInfo.getIsAdministrator()) {
            String alldataRoleId = permissionsConfig.getAlldataRoleid();
            List<String> roleIds = userInfo.getRoleIds();
            if (roleIds == null || (StringUtils.isNotBlank(alldataRoleId) && !roleIds.contains(alldataRoleId))) {
                String userId = userInfo.getUserId();
                //创建人看本人数据；主责部门负责人看本部门数据；主责部门主管领导看所管理部门数据；
                zXGL_RenWuZhuanBanQueryWrapper.lambda().and(wrapper -> wrapper
                        .eq(ZXGL_RenWuZhuanBanEntity::getCreatorUserId, userId)//创建人看本人数据
                        .or()
                        .eq(ZXGL_RenWuZhuanBanEntity::getDepartmentManagerZzbm, userId)//主责部门负责人看本部门数据
                        .or()
                        .eq(ZXGL_RenWuZhuanBanEntity::getDepartmentHeadleaderZzbm, userId) //主责部门主管领导看所管理部门数据
                );
            }
        }
        int zXGL_RenWuZhuanBanNum = 0;
        if (StringUtil.isNotEmpty(zXGL_RenWuZhuanBanPagination.getSqr())) {
            zXGL_RenWuZhuanBanNum++;
            zXGL_RenWuZhuanBanQueryWrapper.lambda().eq(ZXGL_RenWuZhuanBanEntity::getSqr, zXGL_RenWuZhuanBanPagination.getSqr());
        }

        if (StringUtil.isNotEmpty(zXGL_RenWuZhuanBanPagination.getSqbm())) {
            zXGL_RenWuZhuanBanNum++;
            zXGL_RenWuZhuanBanQueryWrapper.lambda().eq(ZXGL_RenWuZhuanBanEntity::getSqbm, zXGL_RenWuZhuanBanPagination.getSqbm());
        }

        if (StringUtil.isNotEmpty(zXGL_RenWuZhuanBanPagination.getZbrq())) {
            zXGL_RenWuZhuanBanNum++;
            List<String> ZbrqList = zXGL_RenWuZhuanBanPagination.getZbrq();
            Long fir = Long.valueOf(ZbrqList.get(0));
            Long sec = Long.valueOf(ZbrqList.get(1));

            zXGL_RenWuZhuanBanQueryWrapper.lambda().ge(ZXGL_RenWuZhuanBanEntity::getZbrq, new Date(fir))
                    .le(ZXGL_RenWuZhuanBanEntity::getZbrq, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (StringUtil.isNotEmpty(zXGL_RenWuZhuanBanPagination.getZbrw())) {
            zXGL_RenWuZhuanBanNum++;
            zXGL_RenWuZhuanBanQueryWrapper.lambda().like(ZXGL_RenWuZhuanBanEntity::getZbrw, zXGL_RenWuZhuanBanPagination.getZbrw());
        }

        if (AllIdList.size() > 0) {
            zXGL_RenWuZhuanBanQueryWrapper.lambda().in(ZXGL_RenWuZhuanBanEntity::getId, AllIdList);
        }
        /**
         * 数据权限 此处手动写入菜单id
         */
    /*String moduleId = "";
    if (!userProvider.get().getIsAdministrator()){
    Object conditionObj = authorizeService.getCondition(zXGL_RenWuZhuanBanQueryWrapper,userProvider.get(),moduleId);
        if (ObjectUtil.isEmpty(conditionObj)){
        return new ArrayList<>();
        }
            zXGL_RenWuZhuanBanQueryWrapper=(QueryWrapper<ZXGL_RenWuZhuanBanEntity>)authorizeService.getCondition( zXGL_RenWuZhuanBanQueryWrapper,userProvider.get(),moduleId);
        }*/
        //排序
//        if (StringUtil.isEmpty(zXGL_RenWuZhuanBanPagination.getSidx())) {
//            zXGL_RenWuZhuanBanQueryWrapper.lambda().orderByDesc(ZXGL_RenWuZhuanBanEntity::getId);
//        } else {
//            try {
//                String sidx = zXGL_RenWuZhuanBanPagination.getSidx();
//                ZXGL_RenWuZhuanBanEntity zXGL_RenWuZhuanBanEntity = new ZXGL_RenWuZhuanBanEntity();
//                Field declaredField = zXGL_RenWuZhuanBanEntity.getClass().getDeclaredField(sidx);
//                declaredField.setAccessible(true);
//                String value = declaredField.getAnnotation(TableField.class).value();
//                zXGL_RenWuZhuanBanQueryWrapper = "asc".equals(zXGL_RenWuZhuanBanPagination.getSort().toLowerCase()) ? zXGL_RenWuZhuanBanQueryWrapper.orderByAsc(value) : zXGL_RenWuZhuanBanQueryWrapper.orderByDesc(value);
//            } catch (NoSuchFieldException e) {
//                e.printStackTrace();
//            }
//        }
        zXGL_RenWuZhuanBanQueryWrapper.lambda().orderByDesc(ZXGL_RenWuZhuanBanEntity::getCreatorTime);
        if ((total > 0 && AllIdList.size() > 0) || total == 0) {
            Page<ZXGL_RenWuZhuanBanEntity> page = new Page<>(zXGL_RenWuZhuanBanPagination.getCurrentPage(), zXGL_RenWuZhuanBanPagination.getPageSize());
            IPage<ZXGL_RenWuZhuanBanEntity> userIPage = this.page(page, zXGL_RenWuZhuanBanQueryWrapper);
            return zXGL_RenWuZhuanBanPagination.setData(userIPage.getRecords(), userIPage.getTotal());
        } else {
            List<ZXGL_RenWuZhuanBanEntity> list = new ArrayList();
            return zXGL_RenWuZhuanBanPagination.setData(list, list.size());
        }
    }

    @Override
    public List<ZXGL_RenWuZhuanBanEntity> getTypeList(ZXGL_RenWuZhuanBanPagination zXGL_RenWuZhuanBanPagination, String dataType) {
        String userId = userProvider.get().getUserId();
        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_RenWuZhuanBanEntity> zXGL_RenWuZhuanBanQueryWrapper = new QueryWrapper<>();
        int zXGL_RenWuZhuanBanNum = 0;
        if (StringUtil.isNotEmpty(zXGL_RenWuZhuanBanPagination.getSqr())) {
            zXGL_RenWuZhuanBanNum++;
            zXGL_RenWuZhuanBanQueryWrapper.lambda().eq(ZXGL_RenWuZhuanBanEntity::getSqr, zXGL_RenWuZhuanBanPagination.getSqr());
        }

        if (StringUtil.isNotEmpty(zXGL_RenWuZhuanBanPagination.getSqbm())) {
            zXGL_RenWuZhuanBanNum++;
            zXGL_RenWuZhuanBanQueryWrapper.lambda().eq(ZXGL_RenWuZhuanBanEntity::getSqbm, zXGL_RenWuZhuanBanPagination.getSqbm());
        }

        if (StringUtil.isNotEmpty(zXGL_RenWuZhuanBanPagination.getZbrq())) {
            zXGL_RenWuZhuanBanNum++;
            List<String> ZbrqList = zXGL_RenWuZhuanBanPagination.getZbrq();
            Long fir = Long.valueOf(ZbrqList.get(0));
            Long sec = Long.valueOf(ZbrqList.get(1));

            zXGL_RenWuZhuanBanQueryWrapper.lambda().ge(ZXGL_RenWuZhuanBanEntity::getZbrq, new Date(fir))
                    .le(ZXGL_RenWuZhuanBanEntity::getZbrq, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (StringUtil.isNotEmpty(zXGL_RenWuZhuanBanPagination.getZbrw())) {
            zXGL_RenWuZhuanBanNum++;
            zXGL_RenWuZhuanBanQueryWrapper.lambda().like(ZXGL_RenWuZhuanBanEntity::getZbrw, zXGL_RenWuZhuanBanPagination.getZbrw());
        }

        /**
         * 数据权限 此处手动写入菜单id
         */
//        String moduleId = "";
//        if (!userProvider.get().getIsAdministrator()) {
//            Object conditionObj = authorizeService.getCondition(zXGL_RenWuZhuanBanQueryWrapper, userProvider.get(), moduleId);
//            if (ObjectUtil.isEmpty(conditionObj)) {
//                return new ArrayList<>();
//            }
//            zXGL_RenWuZhuanBanQueryWrapper = (QueryWrapper<ZXGL_RenWuZhuanBanEntity>) authorizeService.getCondition(zXGL_RenWuZhuanBanQueryWrapper, userProvider.get(), moduleId);
//        }
        if (AllIdList.size() > 0) {
            zXGL_RenWuZhuanBanQueryWrapper.lambda().in(ZXGL_RenWuZhuanBanEntity::getId, AllIdList);
        }
        //排序
        if (StringUtil.isEmpty(zXGL_RenWuZhuanBanPagination.getSidx())) {
            zXGL_RenWuZhuanBanQueryWrapper.lambda().orderByDesc(ZXGL_RenWuZhuanBanEntity::getId);
        } else {
            try {
                String sidx = zXGL_RenWuZhuanBanPagination.getSidx();
                ZXGL_RenWuZhuanBanEntity zXGL_RenWuZhuanBanEntity = new ZXGL_RenWuZhuanBanEntity();
                Field declaredField = zXGL_RenWuZhuanBanEntity.getClass().getDeclaredField(sidx);
                declaredField.setAccessible(true);
                String value = declaredField.getAnnotation(TableField.class).value();
                zXGL_RenWuZhuanBanQueryWrapper = "asc".equals(zXGL_RenWuZhuanBanPagination.getSort().toLowerCase()) ? zXGL_RenWuZhuanBanQueryWrapper.orderByAsc(value) : zXGL_RenWuZhuanBanQueryWrapper.orderByDesc(value);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        if ("0".equals(dataType)) {
            if ((total > 0 && AllIdList.size() > 0) || total == 0) {
                Page<ZXGL_RenWuZhuanBanEntity> page = new Page<>(zXGL_RenWuZhuanBanPagination.getCurrentPage(), zXGL_RenWuZhuanBanPagination.getPageSize());
                IPage<ZXGL_RenWuZhuanBanEntity> userIPage = this.page(page, zXGL_RenWuZhuanBanQueryWrapper);
                return zXGL_RenWuZhuanBanPagination.setData(userIPage.getRecords(), userIPage.getTotal());
            } else {
                List<ZXGL_RenWuZhuanBanEntity> list = new ArrayList();
                return zXGL_RenWuZhuanBanPagination.setData(list, list.size());
            }
        } else {
            return this.list(zXGL_RenWuZhuanBanQueryWrapper);
        }
    }

    @Override
    public ZXGL_RenWuZhuanBanEntity getInfo(String id) {
        QueryWrapper<ZXGL_RenWuZhuanBanEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZXGL_RenWuZhuanBanEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void create(ZXGL_RenWuZhuanBanEntity entity) {
        this.save(entity);
    }

    @Override
    public boolean update(String id, ZXGL_RenWuZhuanBanEntity entity) {
        entity.setId(id);
        return this.updateById(entity);
    }

    @Override
    public OrganizeEntity getById(String organizeId) {
        return null;
    }

    @Override
    public boolean updateFlow(String id) {
        ZXGL_RenWuZhuanBanEntity zxglRenWuZhuanBanEntity = baseMapper.selectById(id);
        if (null != zxglRenWuZhuanBanEntity) {
            ZXGL_XiangMuFenJie_mingxibiaoEntity zxglXiangMuFenJieMingxibiaoEntity = zxglXiangMuFenJieMingxibiaoService.getById(zxglRenWuZhuanBanEntity.getMainid());
            if (null != zxglRenWuZhuanBanEntity.getBzbr()) {
                zxglXiangMuFenJieMingxibiaoEntity.setZrr(zxglRenWuZhuanBanEntity.getBzbr());
            }
            if (null != zxglRenWuZhuanBanEntity.getBzbrbm()) {
                zxglXiangMuFenJieMingxibiaoEntity.setZrbm(zxglRenWuZhuanBanEntity.getBzbrbm());
            }
            if (null != zxglRenWuZhuanBanEntity.getZbhxbr()) {
                zxglXiangMuFenJieMingxibiaoEntity.setXzr(zxglRenWuZhuanBanEntity.getZbhxbr());
            }
            if (null != zxglRenWuZhuanBanEntity.getZbhxbrbm()) {
                zxglXiangMuFenJieMingxibiaoEntity.setXzbm(zxglRenWuZhuanBanEntity.getZbhxbrbm());
            }

            return zxglXiangMuFenJieMingxibiaoService.updateById(zxglXiangMuFenJieMingxibiaoEntity);

        }

        return false;
    }

    @Override
    public void delete(ZXGL_RenWuZhuanBanEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }
    }
    //子表方法

    //列表子表数据方法


    /***
     * 转办流程结束后更信息任务库
     * 被转办人
     * 被转办人部门
     * 转办后协办人
     * 转办后协办人部门
     * @param zxglRenWuZhuanBanInfoVO
     * @return
     */


    @Override
    public boolean updateFenJieMingXI(ZXGL_RenWuZhuanBanInfoVO zxglRenWuZhuanBanInfoVO) {
        ZXGL_RenWuZhuanBanEntity zxglRenWuZhuanBanEntity = baseMapper.selectById(zxglRenWuZhuanBanInfoVO.getTaskId());

        if (ObjectUtil.isNotEmpty(zxglRenWuZhuanBanEntity)) {
            ZXGL_XiangMuFenJie_mingxibiaoEntity fenJieMingxibiao = new ZXGL_XiangMuFenJie_mingxibiaoEntity();
            fenJieMingxibiao.setId(zxglRenWuZhuanBanEntity.getMainid());
            fenJieMingxibiao.setZrr(zxglRenWuZhuanBanEntity.getBzbr());
            fenJieMingxibiao.setZrbm(zxglRenWuZhuanBanEntity.getBzbrbm());
            fenJieMingxibiao.setXzr(zxglRenWuZhuanBanEntity.getZbhxbr());
            fenJieMingxibiao.setXzbm(zxglRenWuZhuanBanEntity.getZbhxbrbm());
            zxglXiangMuFenJieMingxibiaoService.updateById(fenJieMingxibiao);
        }
        return true;
    }
}